// store.js  
import Vue from 'vue';  
import Vuex from 'vuex';  
  
Vue.use(Vuex);  
  
export default new Vuex.Store({  
  state: {  
    cart: {  
      phone: {  
        price: 1000,  
        quantity: 0  
      },  
      computer: {  
        price: 2000,  
        quantity: 0  
      },  
      keyboard: {  
        price: 200,  
        quantity: 0  
      }  
    },  
    totalPrice: 0  
  },  
  getters: {  
    computedTotalPrice: state => {  
      let total = 0;  
      for (let item in state.cart) {  
        if (state.cart.hasOwnProperty(item)) {  
          total += state.cart[item].price * state.cart[item].quantity;  
        }  
      }  
      return total;  
    }  
  },  
  mutations: {  
    INCREMENT_ITEM(state, payload) {  
      if (state.cart[payload.item]) {  
        state.cart[payload.item].quantity++;  
        state.totalPrice += state.cart[payload.item].price;  
      }  
    },  
    DECREMENT_ITEM(state, payload) {  
      if (state.cart[payload.item] && state.cart[payload.item].quantity > 0) {  
        state.cart[payload.item].quantity--;  
        state.totalPrice -= state.cart[payload.item].price;  
      }  
    }  
  }  
});